package org.ielts.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;

import org.apache.commons.lang.StringUtils;
import org.ielts.domain.Sex;
import org.ielts.domain.base.Campus;
import org.ielts.domain.base.Course;
import org.ielts.domain.base.Lesson;
import org.ielts.domain.base.SignCourse;
import org.ielts.domain.base.Student;
import org.ielts.domain.log.AppointConsultorLog;
import org.ielts.domain.log.AppointTutorLog;
import org.ielts.domain.log.ConsultLog;
import org.ielts.domain.log.FirstRecordLog;
import org.ielts.domain.log.OrderLessonLog;
import org.ielts.domain.log.SignCourseLog;
import org.ielts.domain.security.User;
import org.ielts.profile.IeltsProfile;
import org.ielts.repository.AppointConsultorLogRepository;
import org.ielts.repository.AppointTutorLogRepository;
import org.ielts.repository.ConsultLogRepository;
import org.ielts.repository.CourseRepository;
import org.ielts.repository.FirstRecordLogRepository;
import org.ielts.repository.LessonRepository;
import org.ielts.repository.OrderLessonLogRepository;
import org.ielts.repository.SignCourseLogRepository;
import org.ielts.repository.SignCourseRepository;
import org.ielts.repository.StudentRepository;
import org.ielts.repository.security.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.yifeng.ProfileContext;
import com.yifeng.profile.Profile;

@Service
public class FrontService {

	@Autowired
	StudentRepository studentRepository;

	@Autowired
	FirstRecordLogRepository firstRecordLogRepository;

	@Autowired
	ConsultLogRepository consultLogRepository;

	@Autowired
	UserRepository userRepository;

	@Autowired
	AppointConsultorLogRepository appointConsultorLogRepository;

	@Autowired
	AppointTutorLogRepository appointTutorLogRepository;

	@Transactional
	public FirstRecordLog firstVisit(Student student, String source, String title, String front) {
		studentRepository.save(student);

		FirstRecordLog firstRecordLog = new FirstRecordLog();
		firstRecordLog.setStudent(student);
		firstRecordLog.setCreateTime(new Date());
		firstRecordLog.setSource(source);
		firstRecordLog.setTitle(title);

		String campusId = ((IeltsProfile) Profile.getInstance().getProfile()).getCampus();
		if (campusId != null) {
			Campus campus = new Campus();
			campus.setId(campusId);

			firstRecordLog.setCampus(campus);
		}

		if (StringUtils.isNotBlank(front)) {
			User fronter = userRepository.findOne(front);
			firstRecordLog.setCreator(fronter);
		} else {
			User user = (User) Profile.getInstance().getUser();
			firstRecordLog.setCreator(user);
		}
		firstRecordLogRepository.save(firstRecordLog);

		return firstRecordLog;
	}

	@Transactional
	public FirstRecordLog firstVisit(String no, String name, Sex sex, String source) {
		Student student = new Student();
		student.setName(name);
		student.setNo(no);
		student.setSex(sex);

		studentRepository.save(student);

		FirstRecordLog firstRecordLog = new FirstRecordLog();

		String campusId = ((IeltsProfile) Profile.getInstance().getProfile()).getCampus();
		if (campusId != null) {
			Campus campus = new Campus();
			campus.setId(campusId);

			firstRecordLog.setCampus(campus);
		}

		firstRecordLog.setStudent(student);
		firstRecordLog.setCreateTime(new Date());
		firstRecordLog.setSource(source);

		User user = (User) Profile.getInstance().getUser();

		firstRecordLog.setCreator(user);
		firstRecordLogRepository.save(firstRecordLog);

		return firstRecordLog;
	}

	@Transactional
	public AppointConsultorLog appointConsultor(String studentId, String consultId, String title) {

		User consultor = userRepository.findOne(consultId);

		Student student = studentRepository.findOne(studentId);
		// 设置学员新的咨询师
		student.setConsultor(consultor);

		AppointConsultorLog appointConsultorLog = new AppointConsultorLog();
		appointConsultorLog.setConsultor(consultor);
		appointConsultorLog.setStudent(student);

		User creator = (User) Profile.getInstance().getUser();
		appointConsultorLog.setCreator(creator);

		appointConsultorLog.setTitle(title);

		appointConsultorLogRepository.save(appointConsultorLog);

		return appointConsultorLog;
	}

	@Transactional
	public AppointTutorLog appointTutor(String studentId, String tutorId, String title) {
		User tutor = userRepository.findOne(tutorId);

		Student student = studentRepository.findOne(studentId);
		// 设置学员新的咨询师
		student.setTutor(tutor);

		AppointTutorLog appointTutorLog = new AppointTutorLog();
		appointTutorLog.setTutor(tutor);
		appointTutorLog.setStudent(student);

		User creator = (User) Profile.getInstance().getUser();
		appointTutorLog.setCreator(creator);
		appointTutorLog.setTitle(title);

		appointTutorLogRepository.save(appointTutorLog);

		return appointTutorLog;
	}

	@Autowired
	SignCourseLogRepository signCourseLogRepository;

	@Autowired
	OrderLessonLogRepository orderLessonLogRepository;

	@Autowired
	LessonRepository lessonRepository;

	@Autowired
	SignCourseRepository signCourseRepository;

	@Transactional
	public Collection<OrderLessonLog> orderLesson(String signCourseId, String[] lessonIds) {
		User creator = (User) Profile.getInstance().getUser();
		SignCourse signCourse = signCourseRepository.findOne(signCourseId);

		ArrayList<OrderLessonLog> logs = new ArrayList<OrderLessonLog>();

		for (String lessonId : lessonIds) {

			OrderLessonLog orderLessonLog = new OrderLessonLog();
			orderLessonLog.setCreator(creator);

			orderLessonLog.setSignCourse(signCourse);
			orderLessonLog.setStudent(signCourse.getStudent());

			Lesson lesson = lessonRepository.findOne(lessonId);

			orderLessonLog.setLesson(lesson);

			orderLessonLogRepository.save(orderLessonLog);

			logs.add(orderLessonLog);
		}

		return logs;
	}

	@Transactional
	public Collection<OrderLessonLog> findOrderLessonLog(String signCourseId, String[] lessonIds) {
		Collection<OrderLessonLog> orderLessonLogs = orderLessonLogRepository.findBySignCourseAndLesson(signCourseId,
				lessonIds);

		return orderLessonLogs;
	}

	@Transactional
	public OrderLessonLog cancelOrderLesson(String id) {
		OrderLessonLog orderLessonLog = orderLessonLogRepository.findOne(id);
		orderLessonLogRepository.delete(orderLessonLog);

		return orderLessonLog;
	}

	@Autowired
	CourseRepository courseRepository;

	@Transactional
	public ConsultLog consult(String id, String courseId) {

		Student student = studentRepository.findOne(id);
		Course course = courseRepository.findOne(courseId);

		ConsultLog consultLog = new ConsultLog();
		consultLog.setCreateTime(new Date());

		User user = (User) Profile.getInstance().getUser();
		consultLog.setCreator(user);
		consultLog.setStudent(student);
		consultLog.setCourse(course);
		consultLog.setSource(null);
		consultLog.setTitle(null);

		consultLogRepository.save(consultLog);

		return consultLog;
	}

	@Transactional
	public OrderLessonLog orderAuditionLesson(String studentId, String lessonId) {
		User creator = (User) Profile.getInstance().getUser();
		Student student = studentRepository.findOne(studentId);

		OrderLessonLog orderLessonLog = new OrderLessonLog();
		orderLessonLog.setCreator(creator);

		orderLessonLog.setStudent(student);

		Lesson lesson = lessonRepository.findOne(lessonId);

		orderLessonLog.setLesson(lesson);

		orderLessonLogRepository.save(orderLessonLog);

		return orderLessonLog;
	}

	@Transactional
	public List<OrderLessonLog> findOrderLessonLogByLessonAndStudent(String lessonId, String studentId) {
		return orderLessonLogRepository.findOrderLessonLogByLessonAndStudent(lessonId, studentId);
	}

	@Transactional
	public OrderLessonLog orderLesson(String signCourseId, String lessonId) {
		User creator = (User) Profile.getInstance().getUser();
		SignCourse signCourse = signCourseRepository.findOne(signCourseId);

		OrderLessonLog orderLessonLog = new OrderLessonLog();
		orderLessonLog.setCreator(creator);

		orderLessonLog.setSignCourse(signCourse);
		orderLessonLog.setStudent(signCourse.getStudent());

		Lesson lesson = lessonRepository.findOne(lessonId);

		orderLessonLog.setLesson(lesson);

		orderLessonLogRepository.save(orderLessonLog);

		return orderLessonLog;
	}

	@Transactional
	public Iterable<User> findFrontUser() {
		String role = "前台";
		Iterable<User> users = userRepository.findByRoleName(role);
		return users;
	}

	@Transactional
	public Iterable<User> listFront() {
		String role = "前台";
		Iterable<User> users = userRepository.findByRoleName(role);
		return users;
	}
}
